﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    ConditionData.cs
//
// Abstract:
//    
//    Helper class to store controls associated with a notification condition.
//
//--

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Helper class to group the UI controls and a condition in one place.
    /// </summary>
    internal sealed class ConditionData
    {
        private readonly NotificationCondition _condition;
        private readonly UIElement _panel;
        private readonly CheckBox _checkbox;
        private readonly TextBlock _textBlock;

        private static readonly Brush HighlightConditionColor = Brushes.LightGoldenrodYellow;
        private readonly Brush _defaultBrush;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <param name="checkbox">The checkbox which is checked to select the condition.</param>
        /// <param name="textBlock">The textblock describing the condition.</param>
        /// <param name="panel">The panel containing the UI control to edit that condition.</param>
        /// <param name="displayPanel">True if the panel should be displayed; false otherwise.</param>
        public ConditionData(NotificationCondition condition, CheckBox checkbox, TextBlock textBlock, UIElement panel, bool displayPanel)
        {
            Debug.Assert(condition != null, "condition");
            Debug.Assert(panel != null, "panel");
            Debug.Assert(checkbox != null, "checkbox");
            Debug.Assert(textBlock != null, "textBlock");

            _condition = condition;

            // Assign the checkbox and set its initial stat.
            _checkbox = checkbox;
            _checkbox.IsChecked = condition.IsSelected;

            // Assign the initial text (truncating at 60 characters) for the textblock and 
            // highlight if checked.
            _textBlock = textBlock;
            _defaultBrush = _textBlock.Background;

            _panel = panel;

            if (displayPanel)
            {
                Show();
            }
            else
            {
                Hide();
            }
        }

        /// <summary>
        /// True if the condition is currently selected; false otherwise.
        /// </summary>
        public bool IsSelected
        {
            set
            {
                Condition.IsSelected = value;
                Checkbox.IsChecked = value;
            }
        }

        /// <summary>
        /// Display the UI control for this condition.
        /// </summary>
        public void Show()
        {
            _textBlock.Background = HighlightConditionColor;
            Panel.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hide the UI control for this condition.
        /// </summary>
        public void Hide()
        {
            _textBlock.Background = _defaultBrush;
            Panel.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// The notification condition.
        /// </summary>
        public NotificationCondition Condition
        {
            get
            {
                Debug.Assert(_condition != null, "_condition");
                return _condition;
            }
        }

        /// <summary>
        /// The condition checkbox.
        /// </summary>
        public CheckBox Checkbox
        {
            get
            {
                Debug.Assert(_checkbox != null, "_checkbox");
                return _checkbox;
            }
        }

        /// <summary>
        /// The condition description.
        /// </summary>
        public TextBlock TextBlock
        {
            get
            {
                Debug.Assert(_textBlock != null, "_textBlock");
                return _textBlock;
            }
        }

        /// <summary>
        /// The panel containing the control to edit the condition.
        /// </summary>
        public UIElement Panel
        {
            get
            {
                Debug.Assert(_panel != null, "_panel");
                return _panel;
            }
        }
    }
}

